﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YJKGeometryFuncs;

namespace YJKLoopSearch.LoopSearch
{
	public class Common
	{
		public static double MMToFeet(double val)
		{
			return val / 304.8;
		}

		public static double FeetToMM(double val)
		{
			return val * 304.8;
		}

		public static void UpdateModel(UIDocument uiDoc, bool autoJoin)
		{
			uiDoc.Document.Regenerate();
			if (autoJoin)
			{
				uiDoc.Document.AutoJoinElements();
			}
			uiDoc.RefreshActiveView();
		}

		public static List<Level> GetSortLevels(Autodesk.Revit.DB.Document doc, double bottom, double top)
		{
			List<Level> list = new List<Level>();
			foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(Level)).ToElements())
			{
				Level level = element as Level;
				double elevation = level.Elevation;
				if (elevation >= bottom && elevation <= top)
				{
					list.Add(level);
				}
			}
			list.Sort(new LevelComparer());
			return list;
		}

		public static List<XYZ> UniqueAndSortIntersectPoints(Curve curve, List<XYZ> intersectPoints)
		{
			Arc arc = curve as Arc;
			if (null != arc)
			{
				XYZ hyendPoint = arc.GetEndPoint(0);
				if (arc.Normal.Z < 0.0)
				{
					hyendPoint = arc.GetEndPoint(1);
				}
				intersectPoints.Sort(new AngleComparer(hyendPoint, arc.Center));
			}
			Line line = curve as Line;
			if (null != line)
			{
				XYZ hyendPoint2 = line.GetEndPoint(0);
				intersectPoints.Sort(new DistanceComparer(hyendPoint2));
			}
			IEnumerable<XYZ> enumerable = intersectPoints.Distinct(new Comparint<XYZ>((XYZ x, XYZ y) => x != null && y != null && x.DistanceTo(y) < 0.001));
			List<XYZ> list = new List<XYZ>();
			foreach (XYZ item in enumerable)
			{
				list.Add(item);
			}
			return list;
		}

		public static int GetInt(double a)
		{
			if (a == 0.0)
			{
				return 0;
			}
			double num;
			if (a > 0.0)
			{
				num = a + 0.99999999999999989;
			}
			else
			{
				num = a + -0.999999999999999;
			}
			return (int)num;
		}

		private const double _eps = 1E-09;

		public class GridAndInfos
		{
			public GridAndInfos(int nummber, Grid grid)
			{
				this.m_Nummber = nummber;
				this.m_Grid = grid;
			}

			public static bool operator <(Common.GridAndInfos g1, Common.GridAndInfos g2)
			{
				return g1.m_Nummber < g2.m_Nummber;
			}

			public static bool operator >(Common.GridAndInfos g1, Common.GridAndInfos g2)
			{
				return g1.m_Nummber > g2.m_Nummber;
			}

			public static bool operator !=(Common.GridAndInfos g1, Common.GridAndInfos g2)
			{
				return g1.m_Nummber != g2.m_Nummber;
			}

			public static bool operator ==(Common.GridAndInfos g1, Common.GridAndInfos g2)
			{
				return g1.m_Nummber == g2.m_Nummber;
			}

			public override int GetHashCode()
			{
				return (this.m_Nummber ^ this.m_Nummber >> 16) * 352654597;
			}

			public bool Equals(Common.GridAndInfos other)
			{
				return !(other == null) && this.m_Grid == other.m_Grid && this.m_Nummber == other.m_Nummber;
			}

			public override bool Equals(object obj)
			{
				return this.Equals((Common.GridAndInfos)obj);
			}

			public int m_Nummber;

			public Grid m_Grid;
		}
	}
}
